/**
 * Copyright sindney ( http://sindney.googlecode.com )
 * CreativeCommons BY-SA-NC-3.0 License ( http://creativecommons.org/licenses/by-sa-nc/3.0/ )
 */
package  lxq.away3d.controller;

import away3d.core.base.Object3D;
import away3d.entities.Entity;

import flash.display.InteractiveObject;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.ui.MouseCursor;

/**
 * ObjectController
 * Work with away3d broomstick
 * 
 * @version 0.1.1
 * @date 2011/6/12 20:51
 * @author sindney
 */
class SimpleController {

public var target:Object3D;
public var speed:Float = 10;
public var speedMultiplier:Float = 20;
public var sensitive:Float = 1;

private var keys:Array;
private var keyCode:Int = -1;
private var angleS:Float;

private var stage:InteractiveObject;

public function new(target:Entity, stage:Stage,moveSpeed:Float=10,angleSpeed:Float=1) {
	
	this.target = target;
	this.stage = stage;
	this.speed=moveSpeed;
	this.sensitive=angleSpeed;
	this.speedMultiplier=speed+10;
	keys = new Array();
	stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
	stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
	stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	stage.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel);
}

public function update():Void {
	if (keys[Keyboard.A] || keys[Keyboard.UP]) target.moveLeft(v);
	if (keys[Keyboard.D] || keys[Keyboard.DOWN]) target.moveRight(v);
	if (keys[Keyboard.W] || keys[Keyboard.LEFT]) target.moveForward(v);
	if (keys[Keyboard.S] || keys[Keyboard.RIGHT]) target.moveBackward(v);
}

private function onMouseWheel(e:MouseEvent=null):Void{
	e.delta>0?target.moveForward(speed*10):target.moveBackward(speed*10);
}

private function getv():Float {
	return keys[Keyboard.SHIFT] ? speedMultiplier : speed;
}

private function onKeyDown(e:KeyboardEvent):Void {
	keys[e.keyCode] = true;
}

private function onKeyUp(e:KeyboardEvent):Void {
	keys[e.keyCode] = false;
}

private var _mouseEnabled:Bool = false;

public var mouseEnabled(getMouseEnabled, null):Bool;
 	private function getMouseEnabled():Bool {
	return _mouseEnabled;
}

private var oldX:Float;
private var oldY:Float;
private var moving:Bool;

private function onMouseDown(e:MouseEvent):Void {
	//Mouse.cursor=MouseCursor.HAND;
	stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
	stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	moving = true;
	oldX = e.stageX;
	oldY = e.stageY;
}

private function onMouseUp(e:MouseEvent):Void {
	//Mouse.cursor=MouseCursor.AUTO;
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
	stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	moving = false;
}

private function onMouseMove(e:MouseEvent):Void {
	//Mouse.cursor=MouseCursor.HAND;
	target.rotationY += -(oldX - e.stageX) * sensitive;
	target.rotationX += -(oldY - e.stageY) * sensitive;
	if (target.rotationX >= 90) target.rotationX = 90;
	if (target.rotationX <= -90) target.rotationX = -90;
	oldX = e.stageX;
	oldY = e.stageY;
	
}
}